﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using NCS.Library.Components.Security;
using NCS.Library.Components.Users;
using NCS.Library.Utilities;

namespace NCS.Module.CMS.Web.HomePage
{
	public partial class UserPassword : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			lblUpdateStatus.Text = string.Empty;
			
            if (!Page.IsPostBack)
			{
                UserInfo user = this.CurrentUser;
				if (user == null)
				{
					this.btnUpdate.Visible = false;
					return;
				}
				lblUsername.Text = user.Username;
			}
		}
		protected void btnUpdate_Click(object sender, EventArgs e)
		{
			if (txtPassword.Text.Trim() != string.Empty && txtPassword.Text.Trim() == txtPasswordConfirm.Text.Trim())
			{
				try
				{
					UserController.ResetPassword(this.CurrentUser.UserID, SecurityMethod.MD5Encrypt(txtPassword.Text.Trim()));
					lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
				}
				catch (Exception ex)
				{
					lblUpdateStatus.Text = ex.Message;
				}
			}else
			{
				lblUpdateStatus.Text = "Mật khẩu không đúng";
			}
		}
	}
}